Skip to content

Add MockObjectArgCreateStubToCreateMockRector - #741

Merged
TomasVotruba merged 2 commits into
mainfrom
mock-object-arg-create-stub-to-create-mock
Jul 31, 2026
Merged

Add MockObjectArgCreateStubToCreateMockRector#741
TomasVotruba merged 2 commits into
mainfrom
mock-object-arg-create-stub-to-create-mock

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Stub/mock rules move createMock()createStub() when a mock has no expectations. But a variable can be passed to a local helper method that requires MockObject and calls expects() on it — there a stub is a type error.

This rule fixes those spots back:

 public function testNotificationOfUnpublishToAuthor(): void
 {
     $event = new Event();
-    $user  = $this->createStub(User::class);
+    $user  = $this->createMock(User::class);
     $this->prepareCommonMocks($event, $user);
 }

 /**
  * @param MockObject&User $user
  */
 private function prepareCommonMocks(Event $event, MockObject $user): void
 {
     $user->expects($this->once())
         ->method('getId')
         ->willReturn(1);
 }

Real-world case: mautic/mautic NotificationHelperTest

Only triggers when the variable is passed as an argument to a method/static call whose parameter type is MockObject — native type, docblock intersection, positional or named argument. A stub used only for method()/willReturn() is left alone.

Registered in phpunit110.php and phpunit-mock-to-stub.php sets. No ping-pong with ExpressionCreateMockToCreateStubRector, which already skips variables passed to MockObject params.

@TomasVotruba
TomasVotruba merged commit 7294f73 into main Jul 31, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the mock-object-arg-create-stub-to-create-mock branch July 31, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant